package org.alex.samples.service;

import java.util.ArrayList;
import java.util.List;

import org.alex.samples.dao.IExamSubjectDAO;
import org.alex.samples.dao.IExamTestDAO;
import org.alex.samples.javabean.ExamSubjectVO;
import org.alex.samples.model.ExamSubject;
import org.alex.samples.model.ExamTest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ExamSubjectServiceImpl implements IExamSubjectService {

	@Autowired
	private IExamSubjectDAO iExamSubjectDAO;

	@Autowired
	private IExamTestDAO iExamTestDAO;

	@Override
	public List<ExamSubjectVO> findAllExamSubject() {
		List<ExamSubject> examSubjects = iExamSubjectDAO.loadAll();
		List<ExamSubjectVO> examSubjectVOs = new ArrayList<ExamSubjectVO>();
		for (int i = 0; i < examSubjects.size(); i++) {
			ExamSubjectVO examSubjectVO = new ExamSubjectVO();
			BeanUtils.copyProperties(examSubjects.get(i), examSubjectVO);
			examSubjectVOs.add(examSubjectVO);
		}
		return examSubjectVOs;
	}

	@Override
	public ExamSubjectVO getSubjectById(Integer subId) {
		ExamSubjectVO examSubjectVO = new ExamSubjectVO();
		ExamSubject examSubject = iExamSubjectDAO.get(subId);
		BeanUtils.copyProperties(examSubject, examSubjectVO);
		return examSubjectVO;
	}

	@Override
	public void addSubject(ExamSubjectVO examSubjectVO) {
		ExamSubject examSubject = new ExamSubject();
		BeanUtils.copyProperties(examSubjectVO, examSubject);
		iExamSubjectDAO.save(examSubject);
	}

	@Override
	public void deleteSubjectById(Integer id) {
		ExamSubject examSubject = iExamSubjectDAO.get(id);
		List<ExamTest> examTests = iExamTestDAO.getAllExamTestBySubId(id);
		if (examSubject != null) {
			iExamSubjectDAO.delete(examSubject);
			iExamTestDAO.deleteAll(examTests);
		}
	}

	@Override
	public void editSubject(ExamSubjectVO examSubjectVO) {
		ExamSubject examSubject = new ExamSubject();
		BeanUtils.copyProperties(examSubjectVO, examSubject);
		iExamSubjectDAO.update(examSubject);
	}

	@Override
	public List<ExamSubjectVO> getExamSubjectVO(String userId) {
		return null;
	}

}
